package com.db.collections;

import java.util.HashMap;
import java.util.Random;

public class IntHashMapTest {
	static final int N = 100;
	/**
	 * @param args
	 * @throws KeyNotFoundException 
	 */
	public static void main(String[] args) {		
		//testMethods(N);
		testMemoryCosumption(N);
	}

	private static void testMemoryCosumption(int n2) {
		long mem1, mem;
		HashMap<Integer, Integer> hashMap;
		System.gc();
		mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		hashMap = new HashMap<>();
		for (int i=0; i<N; i++)
			hashMap.put(i, i);
		System.gc();
		mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		System.out.printf("hashMap: %d%n", mem1 - mem);
		System.out.println(hashMap.size());

		IntHashMap inthashMap;
		System.gc();
		mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		inthashMap = new IntHashMap();
		for (int i=0; i<N; i++) {
			inthashMap.put(i, i);
		}
		System.gc();
		mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		System.out.printf("inthashMap: %d%n", mem1 - mem);
		System.out.println(inthashMap.size());		
	}

	private static void testMethods(int n) {
		IntHashMap map = new IntHashMap();
		
		Random rand = new Random();
		for (int i = 1; i<=n; i++)
		{
			int val = rand.nextInt(n);
			map.put(val, val*val);
		}
		for (int i = 1; i<=n; i++)
		{
			int val = rand.nextInt(n);
			map.put(val, val*val);
		}
		/*for (int i = 1; i<=n; i++)
		{
			map.put(i, i*i);
		}*/
		for (int i = 1; i<=n; i+=2)
		{
			try {
				map.remove(i);
			} catch (KeyNotFoundException e) {
				System.out.println("Could not remove: " + e.getMessage());
			}
		}
		
		for (int i = 1; i<=n; i++)
			try {
				System.out.println(i + " -> " + map.containsKey(i));
				System.out.println(i + " -> " + map.get(i));
			} catch (KeyNotFoundException e) {
				System.out.printf("Could not find value by key %d%n", i);
			}	
	}
}
